﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Speech.Recognition;
using grounding.parser;

namespace BayesFileParser
{
    class Program
    {
        private const string TRAINING_FILES_DIRECTORY = "../../../BayesTrainingFiles";
        private const string CSV_FILE_LOCATION = "../../../MechanicalTurkDataFiles/Batch_662688_batch_results_edited_3.csv";
        //private const string CSV_FILE_LOCATION = @"..\..\..\MechanicalTurkDataFiles\Batch_662688_batch_results_edited.csv";//@"Z:\UW\CSE481\repo\final_project\grounding\MechanicalTurkDataFiles\Batch_662688_batch_results_edited.csv";



        private static BayesClassifier.Classifier bayesClassifier = new BayesClassifier.Classifier();
        
        static void Main(string[] args)
        {

            BayesFileParser fileParser = new BayesFileParser();
            //BayesClassifier.Classifier bayesClassifier = new BayesClassifier.Classifier();

            // Read in all of the data from the CSV file from Amazon Mechanical Turk
            fileParser.ReadFromCSVFile(CSV_FILE_LOCATION);

            // If we want to store this data, we can write it to files. Each category
            // is written to a different file
            fileParser.WriteToFiles(TRAINING_FILES_DIRECTORY);



            /*
            // There are two different options for training the Bayesian model at this point.
            // If you still have the original BayesFileParser, you can read from that.
            foreach (Category cat in fileParser.BayesFileData.Keys)
            {

            }
            */
            // Otherwise, you can read from the files that were written previously. This
            // is what is more likely to happen, since we don't want to retrain a bunch of
            // data we don't need.
            foreach (CategoryName catName in Enum.GetValues(typeof(CategoryName)))
            {
                //string file = @"..\..\Test.txt";
                // Cat1 is a match with the file itself
                //m_Classifier.TeachCategory("Cat1", new System.IO.StreamReader(file));

                if (catName != CategoryName.NONE)
                {

                    Category cat = Category.CategoryNameToCategoryDictionary[catName];

                    bayesClassifier.TeachCategory(cat.Element, new System.IO.StreamReader(
                        BayesFileParser.getFilePath(cat, TRAINING_FILES_DIRECTORY)));
                    bayesClassifier.TeachCategory(cat.Element, new System.IO.StreamReader(
                        BayesFileParser.getFilePath(cat, TRAINING_FILES_DIRECTORY)));
                }

            }



            // Setup SpeechToText
            SpeechToText stt = new SpeechToText();
            stt.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(stt_SpeechRecognized);
            stt.DebugMode = false;

            // Helpful text
            Console.WriteLine("Starting SpeechToTextTest...");
            Console.WriteLine("You should be able to speak a word or sentence and have it classified.");

            // Start recognition
            stt.Start();



            //// Classify our test file (we would classify the spoken input, not a file)
            //string file = TRAINING_FILES_DIRECTORY + "/awake.txt";
            //Dictionary<string, double> score = bayesClassifier.Classify(new System.IO.StreamReader(file));
            //Double max = Double.MinValue; // This is a stupid hack
            //string maxKey = "";
            //foreach (string c in score.Keys)
            //{
            //    if (score[c] > max)
            //    {
            //        max = score[c];
            //        maxKey = c;
            //    }
            //    Console.WriteLine(c + ":" + score[c].ToString());
            //}

            //Console.WriteLine();
            //Console.WriteLine("The best category is: {0}", maxKey);


            Console.ReadLine();
            
        }

        static void stt_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
        {
            Console.WriteLine("Input: {0}", e.Result.Text);

            // Classify our test file (we would classify the spoken input, not a file)
            
            // classification only reads from a file, so write to a temporary file...
            System.IO.File.WriteAllText("temp.txt", e.Result.Text);
            System.IO.StreamReader reader = new System.IO.StreamReader("temp.txt");
            Dictionary<string, double> score = bayesClassifier.Classify(reader);
            reader.Close();

            Double max = Double.MinValue; // This is a stupid hack
            string maxKey = "";
            Console.WriteLine("Category: score");
            
            foreach (string c in score.Keys)
            {
                if (score[c] > max)
                {
                    max = score[c];
                    maxKey = c;
                }
                Console.WriteLine(c + ":" + score[c].ToString());
            }

            Console.WriteLine();
            Console.WriteLine("The best category is: {0}", maxKey);

        }
    }
}
